const execa = require("execa");
const args = require("minimist")(process.argv.slice(2));
const { packagesDir, resolve } = require("./paths");
const chalk = require("chalk");
const fs = require("fs");
const path = require("path");
const log = console.log;
const { rimrafSync } = require("rimraf");
//获取公共包名
const commonPackageName = fs.readdirSync(packagesDir);

commonPackageName.forEach((element) => {
  if (fs.existsSync(path.join(packagesDir, element, "es"))) {
    rimrafSync(path.join(packagesDir, element, "es"));
    log(chalk.red("删除文件:") + path.join(packagesDir, element, "es"));
  }
  if (fs.existsSync(path.join(packagesDir, element, "dist"))) {
    rimrafSync(path.join(packagesDir, element, "dist"));
    log(chalk.red("删除文件:") + path.join(packagesDir, element, "dist"));
  }
});

let env = "dev";
let comName = "react-com";
if (args._.length) {
  comName = args._[0];
  env = args._[1];
}

execa(
  "rollup",
  [
    "-cw",
    "--environment",
    `NODE_ENV:${env}`,
    "--environment",
    `COMNAME:${comName}`,
  ],
  {
    stdio: "inherit", //表示
  }
);

